package com.kantboot.project.file.config;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.kantboot.project.security.service.ISysUserService;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import javax.annotation.Resource;

/**
 * OSS客户端，用来操作OSS
 */
@Data
@Log4j2
@Lazy
@Configuration
public class OSSConfig {

    @Resource
    ISysUserService sysUserService;

    @Value("${oss.endpoint}")
    private String endpoint;

    /**
     * 阿里云账号AccessKey拥有所有API的访问权限，风险很高。
     * 强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
     */
    @Value("${oss.access-key-id}")
    private String accessKeyId;

    @Value("${oss.access-key-secret}")
    private String accessKeySecret ;

    @Value("${oss.bucket-name}")
    private String bucketName;

    @Value("${oss.host}")
    private String host;

    @Value("${oss.callback-url}")
    private String callbackUrl;

    @Bean
    public OSS oss(){
        OSS result = null;

        try{
            result = new OSSClientBuilder()
                    .build(endpoint, accessKeyId, accessKeySecret);
        }catch (OSSException oe){
            log.error("捕获到OSSException，" +
                    "这意味着您的请求向OSS发出，" +
                    "但由于某种原因被错误响应拒绝。");
            log.error("Error Message:" + oe.getErrorMessage());
            log.error("Error Code:" + oe.getErrorCode());
            log.error("Request ID:" + oe.getRequestId());
            log.error("Host ID:" + oe.getHostId());
        }catch (ClientException e){
            log.error("捕获到客户端异常，" +
                    "这意味着客户端在尝试与OSS通信时遇到了严重的内部问题，" +
                    "例如无法访问网络。");
            log.error("Error Message:" + e.getMessage());
        }

        return result;
    }


}
